package fun.yao.common.msg;

import fun.yao.common.Constant;
import fun.yao.common.exception.GessErrCode;
import fun.yao.common.uitls.CommonUtil;
import fun.yao.common.uitls.MsgUtil;

/**
 * 交易类响应报文（包括报文头和报文体）
 */
public class TransRspMsg
{
    //------------------------- 报文头报文长度定义
    public final static int SEQ_NO_LEN    = 8 ;
    public final static int MSG_TYPE_LEN  = 1 ;
    public final static int EXCH_CODE_LEN = 4 ;
    public final static int MSG_FLAG_LEN  = 1 ;
    public final static int TERM_TYPE_LEN = 2 ;
    public final static int USER_TYPE_LEN = 2 ;
    public final static int USER_ID_LEN   = 10;
    public final static int AREA_CODE_LEN = 4 ;
    public final static int BRANCH_ID_LEN = 12 ;
    public final static int SESSION_ID_LEND = 16;
    public final static int RSP_CODE_LEN = 8 ;

    //------------------------- 报文头部分
    /** 流水号    */
    public String seq_no    = "";
    /** 报文类型  */
    public String msg_type  = "";
    /** 交易代码  */
    public String exch_code = "";
    /** 报文标识  */
    public String msg_flag  = "";
    /** 终端来源  */
    public String term_type = "";
    /** 用户类型  */
    public String user_type = "";
    /** 用户ID    */
    public String user_id   = "";
    /** 地区代码  */
    public String area_code = "";
    /** 代理机构  */
    public String branch_id = "";
    /**session id*/
    public String session_id = "";
    /** 响应代码 */
    public String rsp_code = Constant.RSP_CODE_SUCCEED;
    /** 响应消息 */
    public String rsp_msg = "处理成功";

    /**报文头类型 为区分二级系统老报文头*/
    public boolean is_new = true;

    //------------------------- 报文体
    public StringBuffer sbMsgBody = new StringBuffer();

    /**
     * 解析请求报文
     * @param sReqMsg
     */
    public void Parse(String sReqMsg)
    {
        //通过报文头长度兼容老报文
        if(sReqMsg.indexOf(Constant.MSG_SEPARATOR_FIELD) < 68)
            this.is_new = false;

        int index = 0 ;
        seq_no    = sReqMsg.substring(index,index + SEQ_NO_LEN   ).trim();
        index += SEQ_NO_LEN    ;
        msg_type  = sReqMsg.substring(index,index + MSG_TYPE_LEN ).trim();
        index += MSG_TYPE_LEN  ;
        exch_code = sReqMsg.substring(index,index + EXCH_CODE_LEN).trim();
        index += EXCH_CODE_LEN ;
        msg_flag  = sReqMsg.substring(index,index + MSG_FLAG_LEN ).trim();
        index += MSG_FLAG_LEN  ;
        term_type = sReqMsg.substring(index,index + TERM_TYPE_LEN).trim();
        index += TERM_TYPE_LEN ;
        user_type = sReqMsg.substring(index,index + USER_TYPE_LEN).trim();
        index += USER_TYPE_LEN ;
        user_id   = sReqMsg.substring(index,index + USER_ID_LEN  ).trim();
        index += USER_ID_LEN   ;
        area_code = sReqMsg.substring(index,index + AREA_CODE_LEN).trim();
        index += AREA_CODE_LEN ;
        branch_id = sReqMsg.substring(index,index + BRANCH_ID_LEN).trim();
        index += BRANCH_ID_LEN ;

        if(is_new)
        {
            session_id = sReqMsg.substring(index,index + SESSION_ID_LEND).trim();
            index += SESSION_ID_LEND ;
        }

        rsp_code  = sReqMsg.substring(index,index + RSP_CODE_LEN).trim();
        index += RSP_CODE_LEN ;

        sbMsgBody.append(sReqMsg.substring(index).trim());

        rsp_msg = MsgUtil.getMsgFieldValue(sReqMsg,"rsp_msg");

    }

    /**
     * 根据请求报文初始化响应报文
     * @param TReqMsg
     */
    public void init(TransReqMsg TReqMsg)
    {
        this.seq_no    = TReqMsg.seq_no;
        this.msg_type  = Constant.MSG_TYPE_TRANS;
        this.exch_code = TReqMsg.exch_code;
        this.msg_flag  = Constant.MSG_FLAG_RSP;
        this.term_type = TReqMsg.term_type;
        this.user_type = TReqMsg.user_type;
        this.user_id   = TReqMsg.user_id;
        this.area_code = TReqMsg.area_code;
        this.branch_id = TReqMsg.branch_id;
        this.session_id = TReqMsg.session_id;
        this.rsp_code  = Constant.RSP_CODE_SUCCEED;
        this.rsp_msg   = "处理成功";

        this.is_new = TReqMsg.is_new;

    }

    /**
     * 获得响应报文的唯一值
     * @return
     */
    public String getUniqueKey()
    {
        StringBuffer sb = new StringBuffer(30);
        sb.append(this.user_type);
        sb.append(":");
        sb.append(this.user_id);
        sb.append(":");
        sb.append(this.seq_no);
        return sb.toString();
    }

    /**
     * 拼组请求报文字符串
     */
    public String toString()
    {
        return toStringBuffer().toString();
    }

    /**
     * 拼组请求报文字符串
     */
    public StringBuffer toStringBuffer()
    {
        StringBuffer sb = new StringBuffer(200);
        sb.append(CommonUtil.FILL(seq_no    ,' ',SEQ_NO_LEN   ,'R'));
        sb.append(CommonUtil.FILL(msg_type  ,' ',MSG_TYPE_LEN ,'R'));
        sb.append(CommonUtil.FILL(exch_code ,' ',EXCH_CODE_LEN,'R'));
        sb.append(CommonUtil.FILL(msg_flag  ,' ',MSG_FLAG_LEN ,'R'));
        sb.append(CommonUtil.FILL(term_type ,' ',TERM_TYPE_LEN,'R'));
        sb.append(CommonUtil.FILL(user_type ,' ',USER_TYPE_LEN,'R'));
        sb.append(CommonUtil.FILL(user_id   ,' ',USER_ID_LEN  ,'R'));
        sb.append(CommonUtil.FILL(area_code ,' ',AREA_CODE_LEN,'R'));
        sb.append(CommonUtil.FILL(branch_id ,' ',BRANCH_ID_LEN,'R'));
        if(is_new)
            sb.append(CommonUtil.FILL(session_id ,' ',SESSION_ID_LEND,'R'));
        sb.append(CommonUtil.FILL(rsp_code  ,' ',RSP_CODE_LEN, 'R'));

        sb.append(Constant.MSG_SEPARATOR_FIELD);
        sb.append("rsp_msg");
        sb.append(Constant.MSG_SEPARATOR_VALUE);
        sb.append(rsp_msg);
        //sb.append(Constant.MSG_SEPARATOR_FIELD);

        if ( sbMsgBody == null || sbMsgBody.length() <= 0 )
            sb.append(Constant.MSG_SEPARATOR_FIELD);
        else
        {
            if ( sbMsgBody.charAt(0) != Constant.MSG_SEPARATOR_FIELD ) //不是以分隔符开头，则增加分隔符
                sb.append(Constant.MSG_SEPARATOR_FIELD);
            sb.append(sbMsgBody.toString());
        }

        return sb;
    }

    /**
     * 获取报文头kv模式
     * 用于写日志索引文件
     * add by ljl 20161123
     * @param lStartTime
     * @return
     */
    public StringBuffer rspHeadToStringBufferKV(long lStartTime)
    {
        long endTime = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer(200);
        sb.append(Constant.MSG_SEPARATOR_FIELD);
        MsgBase.addMsgFieldValue(sb, "seq_no", this.seq_no);
        MsgBase.addMsgFieldValue(sb, "msg_type", this.msg_type);
        MsgBase.addMsgFieldValue(sb, "exch_code", this.exch_code);
        MsgBase.addMsgFieldValue(sb, "msg_flag", this.msg_flag);
        MsgBase.addMsgFieldValue(sb, "term_type", this.term_type);
        MsgBase.addMsgFieldValue(sb, "user_type", this.user_type);
        MsgBase.addMsgFieldValue(sb, "user_id", this.user_id);
        MsgBase.addMsgFieldValue(sb, "area_code", this.area_code);
        MsgBase.addMsgFieldValue(sb, "branch_id", this.branch_id);
        MsgBase.addMsgFieldValue(sb, "session_id", this.session_id);
        MsgBase.addMsgFieldValue(sb, "rsp_code", this.rsp_code);
        MsgBase.addMsgFieldValue(sb, "start_time", String.valueOf(lStartTime));
        MsgBase.addMsgFieldValue(sb, "end_time", String.valueOf(endTime));
        MsgBase.addMsgFieldValue(sb, "deal_time", String.valueOf(endTime - lStartTime));
        return sb;
    }

    /**
     * 检查是否成功
     * @return
     */
    public boolean checkSuccess()
    {
        if(this.rsp_code.equals(GessErrCode.HJ0000) || this.rsp_code.equals(Constant.RSP_CODE_SUCCEED))
            return true;
        else
            return false;
    }

}
